﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace graduation_project.Voice_Recognition
{
    
    public partial class KeyWordsMain : Form
    {
        string wavfile = null;
        private Record rec = null;
        string[] buffer = new string[3];
        public KeyWordsMain()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DynamicAddXml AddXml = new DynamicAddXml();
            if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "")
            {
                MessageBox.Show("请输入关键字！");
            }
            else
            {
                AddXml.XmlBuffer[0] = textBox1.Text ;
                AddXml.XmlBuffer[1] = textBox2.Text ;
                AddXml.XmlBuffer[2] = textBox3.Text ;
                buffer[0] = textBox1.Text;
                buffer[1] = textBox2.Text;
                buffer[2] = textBox3.Text;
                AddXml.UpdateXml();
            }         
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            rec = new Record();
            
            if (textBox4.Text != "")
            {
                wavfile = textBox4.Text+".wav";
                rec.SetFileName(wavfile);
                rec.RecStart(); 
            }
            else
                MessageBox.Show("请输入文件名！");            
        }

        private void button7_Click(object sender, EventArgs e)
        {
            rec.RecStop();
            rec = null;
            MessageBox.Show("音频录入成功！");
        }

        private void button8_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofile = new OpenFileDialog();                                    //打开文件对话框
            ResultTextBox.Clear();
            textBox5.Clear();
            ofile.Filter = "wav files (*.wav)|*.wav|All files(*.*)|*.*";
            ofile.Title = "打开音频文件";
            ofile.RestoreDirectory = true;
            ofile.InitialDirectory = Environment.CurrentDirectory;

            if (ofile.ShowDialog() == DialogResult.OK)
            {
                ReconginiteRecord.instance().RecordFile(ResultTextBox,ofile.FileName);        //识别文件
            }
            Thread CmdRecongnition = new Thread(delegate()
            {
                Thread.Sleep(3000);
                ExecuteCmd executeCmd = new ExecuteCmd();
                if (ResultTextBox.Text == "")
                {
                    MessageBox.Show("没有识别到该文件有您想要的关键字，请重新识别！");    
                }     
            });
            CmdRecongnition.Start();
            if (buffer != null)
            {
                MessageBox.Show("你选择的文件是:" + ofile.FileName.Substring(ofile.FileName.LastIndexOf("\\") + 1, ofile.FileName.Length - 1 - ofile.FileName.LastIndexOf("\\")));
                for (int i = 0; i < 3; i++)
                {
                    if (ResultTextBox.Text.Contains(buffer[i]))
                    {
                        //MessageBox.Show("111");
                        buffer[i] = buffer[i] + " ";
                        textBox5.Text += buffer[i];
                    }
                }
            }
          
            
            

        }


        private void button4_Click(object sender, EventArgs e)
        {
            ResultTextBox.Clear();
            button8_Click(button4, e);
        }

        private void KeyWordsMain_Load(object sender, EventArgs e)
        {
            this.ResultTextBox.Visible = false;
        }
    }
}
